/*
  JavaScript code for popup.html
*/

function WorkaroundXFrameOptionsForTab(tab)
{
	chrome.extension.getBackgroundPage().InitializeXFrameOptionsWorkaround(tab.id);
}

function createCompareTab(mode)
{
	var checkedCount = 0;
	var urls = [];
	var ids = [];
	var tbody = document.getElementById("tabList").children.item(0);
	for(var i = 0; i < tbody.children.length; ++i)
	{
		var cur = tbody.children.item(i);
		var input = cur.children.item(0).children.item(0);
		//                           <td>            <input>
		var url_element = cur.children.item(2).children.item(0);
		//                                  <td>             <a> (or possibly <input type="text">)
		if(input.checked)
		{
			ids[checkedCount] = input.id == "newPageCheck" ? null : input.tag_tabId;
			urls[checkedCount] = input.id == "newPageCheck" ? url_element.value : input.tag_url;
			++checkedCount;
		}
	}
	if(checkedCount >= 2)
	{
		compareInfo = 
		{
			direction: mode, 
			urls : [urls[0], urls[1]]
		};
		chrome.tabs.create({url: "compare.html?compareInfo=" + escape(JSON.stringify(compareInfo))}, 
			(function(tab){
				chrome.extension.getBackgroundPage().InitializeXFrameOptionsWorkaround(tab.id)
			}));
		if(document.getElementById("CloseTabs").checked)
		{
			ids[0] && chrome.tabs.remove(ids[0]);
			ids[1] && chrome.tabs.remove(ids[1]);
		}
	}
}

function lrCompare_Click()
{
	createCompareTab("lr");
}

function tbCompare_Click()
{
	createCompareTab("tb");
}

chrome.tabs.getSelected(null, (function(selectedTab){
chrome.tabs.getAllInWindow(null, function(tabs)
{
	for(var i = 0; i < tabs.length; ++i)
	{
		var cur = tabs[i];
		var tr = document.createElement("tr");
		var td_input = document.createElement("td");
		var td_icon = document.createElement("td");
		var td_title = document.createElement("td");
		var input = document.createElement("input");
		var icon = document.createElement("img");
		var title = document.createElement("a");
		input.type = "checkbox";
		input.tag_tabId = cur.id;
		input.tag_url = cur.url;
		input.tag_title = cur.title;
		if(selectedTab && selectedTab.id == cur.id)
			input.checked = true;
		icon.src = "chrome://favicon/" + cur.url;
		icon.onclick = title.onclick = (function(){
			this.parentElement.parentElement.children.item(0).children.item(0).click()
			//    <td>          <tr>                       <td>  <input type="checkbox">
		});
		title.href = "javascript:void(0)"; //Although links in popups of browser actions 
		                                   //will NEVER navigate, we still use a dummy href
		title.appendChild(document.createTextNode(cur.title || cur.url));
		td_title.appendChild(title);
		td_icon.appendChild(icon);
		td_input.appendChild(input);
		tr.appendChild(td_input);
		tr.appendChild(td_icon);
		tr.appendChild(td_title);
		document.getElementById("tabList").children.item(0).appendChild(tr);
	}
});
}));

document.getElementById("direction_lr").onclick = lrCompare_Click;
document.getElementById("direction_tb").onclick = tbCompare_Click;

document.getElementById("newPageURL").oninput = (function(){
	var newPageCheck = document.getElementById("newPageCheck");
	var empty = (document.getElementById("newPageURL").value.length == 0);
	newPageCheck.disabled = empty;
	newPageCheck.checked = !empty;
});
